﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using gsToolkit;
using System.Text.RegularExpressions;

namespace gsToolkit.gSharp {
    public class ScriptProcessor {
        private static Regex RxMultilineMacro = new Regex(@"\#\.(.*?)\#\.", System.Text.RegularExpressions.RegexOptions.Singleline);
        private static Regex RxSingleLineMacro = new Regex(@"\#([^\.].*)", System.Text.RegularExpressions.RegexOptions.Multiline);
        
        private static ScriptProcessor instance;
        private GenerateOptions generateOutput = GenerateOptions.None;
        public string OutputFile { get; set; }
        public bool Debug { get; set; }
        public bool CompileOnly { get; set; }
        private string scriptFilename;
        private string scriptText;
        private MatchCollection singleLineMacros;
        private MatchCollection multiLineMacros;


        public string ScriptFilename { 
            get {
                return scriptFilename;
            } 
            set {
                if( !File.Exists(value) )  
                    throw new FileNotFoundException("Script file not found [{0}]".format(value), value);
                scriptFilename = value;
            }
        }

        public static ScriptProcessor Instance { 
            get {
                if( null == instance ) {
                    instance = new ScriptProcessor();
                }
                return instance;
            }
        }

        public GenerateOptions GenerateOutput {
            get {
                return generateOutput;
            }
            set {
                generateOutput = value;
            }
        }

        internal ScriptProcessor() {

        }
        public void ClearCache() {
               
        }

        public int Process(List<string> parameters) {
            scriptText = File.ReadAllText(scriptFilename);
            singleLineMacros = RxSingleLineMacro.Matches(scriptText);
            multiLineMacros = RxMultilineMacro.Matches(scriptText);

            IdentifyLanguage();
            LoadMacroHandlers();
            
            RunMacros();
            Compile();
            if(!CompileOnly)
                return Run();

            return 0;
        }

        private int Run() {

            return 0;
        }

        private void Compile() {
            

        }

        private void RunMacros() {
        }

        private void LoadMacroHandlers() {

        }

        private void IdentifyLanguage() {
            

        }
    }
}
